package com.octopus.core.downloader.proxy;

import com.octopus.core.Request;
import java.util.List;
import java.util.Random;
import lombok.NonNull;

/**
 * 随机从代理池里面选择一个代理进行下载
 *
 * @author yang_shoulai@hoperun.com
 * @date 2019/08/07
 */
public class RandomProxyProvider implements ProxyProvider {

  private List<Proxy> proxies;

  public RandomProxyProvider(@NonNull List<Proxy> proxies) {
    this.proxies = proxies;
  }

  @Override
  public Proxy provide(Request request) {
    return proxies.isEmpty() ? null : proxies.get(new Random().nextInt(proxies.size()));
  }
}
